<template>
	<view>
		<view class="popup">
			<view class="top">
				<view class="ximg">
					<image :src="good.goodImg" mode="widthFix"></image>
				</view>
				<view>
					<view class="price2">
						<block v-for="(item,index) in price" :key="item">
							<label v-if="index == 0">￥</label>
							<label :style="index == 0 ? 'font-weight: bold;font-size:25px' : ''">
								<label v-if="index != 0">.</label>
								{{item}}
							</label>
						</block>
					</view>
					<view class="xz">
						<label style="color: gray;">已选</label>
						<view class="val">
							<label>{{qua(index)}}</label>
						</view>

						<label>{{num}}个</label>
					</view>
				</view>
			</view>

			<view class="type">
				<view>
					<label>类型</label>
				</view>
				<scroll-view scroll-y scroll-with-animation style="height: 150px;" @touch.stop>
					<view class="btn">
						<block v-for="(item,index) in good.goodquaList" :key="item.id">
							<button size="mini" :disabled="item.issock ? true : false"
								:class="{'button':true , 'active' : activeIndex === index}" @click="btn(index)">
								<label v-if="item.issock">缺|</label>
								{{item.val}}
							</button>
						</block>
					</view>
				</scroll-view>
			</view>


			<view class="sum">
				<view>
					<label>数量</label>
				</view>
				<view class="add">
					<button @click="toggle(-1)">-</button>
					<text>{{num}}</text>
					<button @click="toggle(1)">+</button>
				</view>
			</view>

			<view class="bottom">
				<button @click="to()">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "good-qua",
		props: {
			item: {
				type: Object,
				default () {
					return {}
				}
			}
		},

		data() {
			return {
				good: {},
				index: 0,
				activeIndex: 0,
				num: 1,
			};
		},
		watch: {
			item: {
				handler(newVal, oldVal) {
					this.good = newVal
				},
				immediate: true
			}
		},
		methods: {
			btn(index) {
				this.activeIndex = index;
				this.index = index;
			},
			toggle(i) {
				this.num = this.num + i;
				if (this.num <= 0) {
					this.num = 1;
				}
			},
			to() {
				let data = {
					'val': this.qua(this.index),
					'num': this.num
				};
				this.$emit('get', data)
			}
		},
		computed: {
			qua: function() {
				return (id) => {
					let val = '';
					if (this.good) {
						val = this.good.goodquaList[id].val;
					}
					return val;
				}
			},
			price() {
				let num = this.good.goodPrice;
				if (this.good.goodquaList[this.index].price != 0) {
					num = this.good.goodquaList[this.index].price;
				}
				num = num.toFixed(2)
				num = num.split('.');
				return num;
			},
		},
		mounted() {
			for (let i = 0; i < this.good.goodquaList.length; i++) {
				if (!this.good.goodquaList[i].issock) {
					this.index = i;
					this.activeIndex = i;
					break;
				}
			}
		}
	}
</script>

<style scoped>
	.popup .top .ximg image {
		width: 100px;
	}

	.popup .top {
		display: flex;
		margin-top: 30px;
		align-items: center;
		margin-left: 30px;
	}

	.popup {
		height: 470px;
	}

	.price2 label{
		margin-bottom: 5px;
		color: red;
	}

	.type {
		margin-top: 20px;
	}

	.type label {
		font-size: 25px;
		font-weight: bold;
		margin-left: 25px;
	}

	.type .btn {
		margin-left: 20px;
		margin-top: 20px;
	}

	.type .btn .button {
		margin-top: 10px;
		margin-left: 10px;
		border-radius: 50px;
		color: gray;
	}

	.type .btn .button.active {
		color: red;
		border: 1px solid red;
	}

	.type .btn .button label {
		font-size: 15px;
		color: red;
		margin-left: -10px;
	}

	.sum {
		margin-top: 25px;
		display: flex;
		justify-content: space-between;

	}

	.sum label {
		font-size: 25px;
		font-weight: bold;
		margin-left: 25px;
	}

	.add {
		display: flex;
		align-items: center;
		margin-right: 20px;
	}

	.add button {
		height: 25px;
		display: flex;
		align-items: center;
		margin-left: 5px;
		margin-right: 5px;
	}

	.add text {
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #c3c3c3;
		height: 25px;
		width: 35px;
		background-color: #f8f8f8;

	}

	.bottom {
		margin-top: 25px;
	}

	.bottom button {
		background-color: red;
		width: 80%;
		border-radius: 50px;
		color: white;
	}

	.xz {
		display: flex;
	}

	.xz .val {
		width: 9em;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		margin-left: 10px;
		margin-right: 10px;
	}
</style>